package com.tl.commonlibrary.storage.configuration;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
 * Created by jiangmingsun on 15/4/2.
 */
public class PrefUtil {
    private SharedPreferences preference;

    public PrefUtil(Context context) {
        this(context, context.getPackageName());
    }

    public PrefUtil(Context context, String name) {
        preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    //----------------------------------------------------------------------------------------------
    public void putInt(String key, int value) {
        SharedPreferences.Editor editor = preference.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public void putString(String key, String value) {
        SharedPreferences.Editor editor = preference.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void putFloat(String key, float value) {
        SharedPreferences.Editor editor = preference.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public void putLong(String key, long value) {
        SharedPreferences.Editor editor = preference.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = preference.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    //----------------------------------------------------------------------------------------------
    public String getString(String key) {
        return getString(key, null);
    }

    public String getString(String key, String defaultValue) {
        return preference.getString(key, defaultValue);
    }
    public Map<String, ?> getAll(){
        return preference.getAll();
    }
    public float getFloat(String key) {
        return getFloat(key, 0f);
    }

    public float getFloat(String key, float defaultValue) {
        return preference.getFloat(key, defaultValue);
    }

    public long getLong(String key) {
        return getLong(key, 0l);
    }

    public long getLong(String key, long defaultValue) {
        return preference.getLong(key, defaultValue);
    }

    /**
     * 从共享数据区中取出Boolean数据
     *
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return preference.getBoolean(key, defaultValue);
    }
    /**
     * 从共享数据区中取出整型数据
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        return preference.getInt(key, defaultValue);
    }

    public void remove(String key) {
        SharedPreferences.Editor editor = preference.edit();
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清除共享数据区的缓存数据
     */
    public void removeAll() {
        SharedPreferences.Editor editor = preference.edit();
        editor.clear();
        editor.commit();
    }
}
